package io.kiki.sba.registry.server.clientInterface.converter.pb;

import io.kiki.sba.registry.common.model.client.pb.BaseRegisterPb;
import io.kiki.sba.registry.common.model.client.pb.PublisherRegisterPb;
import io.kiki.sba.registry.core.model.PublisherRegister;


public final class PublisherRegisterConvertor {
    private PublisherRegisterConvertor() {
    }

    public static PublisherRegisterPb convert2Pb(PublisherRegister publisherRegisterJava) {

        if (publisherRegisterJava == null) {
            return null;
        }

        PublisherRegisterPb.Builder builder = PublisherRegisterPb.newBuilder();

        BaseRegisterPb.Builder baseRegisterBuilder = BaseRegisterPb.newBuilder();

        baseRegisterBuilder.setAppName(publisherRegisterJava.getAppName()).setClientId(publisherRegisterJava.getClientId()).setDataId(publisherRegisterJava.getDataId()).setDataInfoId(publisherRegisterJava.getDataInfoId()).setEventType(publisherRegisterJava.getEventType()).setGroup(publisherRegisterJava.getGroup()).setInstanceId(publisherRegisterJava.getInstanceId()).setIp(publisherRegisterJava.getIp()).setPort(publisherRegisterJava.getPort()).setProcessId(publisherRegisterJava.getProcessId()).setRegistId(publisherRegisterJava.getRegistId()).setVersion(publisherRegisterJava.getVersion()).setTimestamp(publisherRegisterJava.getTimestamp()).putAllAttributes(publisherRegisterJava.getAttributes());

        if (publisherRegisterJava.getZone() != null) {
            baseRegisterBuilder.setZone(publisherRegisterJava.getZone());
        }

        builder.setBaseRegister(baseRegisterBuilder.build());

        builder.addAllDataList(DataBoxConvertor.convert2Pbs(publisherRegisterJava.getDataList()));
        return builder.build();
    }

    public static PublisherRegister convert2Java(PublisherRegisterPb publisherRegisterPb) {

        if (publisherRegisterPb == null) {
            return null;
        }

        PublisherRegister publisherRegister = new PublisherRegister();

        publisherRegister.setAppName(publisherRegisterPb.getBaseRegister().getAppName());
        publisherRegister.setClientId(publisherRegisterPb.getBaseRegister().getClientId());
        publisherRegister.setDataId(publisherRegisterPb.getBaseRegister().getDataId());
        publisherRegister.setDataInfoId(publisherRegisterPb.getBaseRegister().getDataInfoId());
        publisherRegister.setEventType(publisherRegisterPb.getBaseRegister().getEventType());
        publisherRegister.setGroup(publisherRegisterPb.getBaseRegister().getGroup());
        publisherRegister.setInstanceId(publisherRegisterPb.getBaseRegister().getInstanceId());
        publisherRegister.setIp(publisherRegisterPb.getBaseRegister().getIp());
        publisherRegister.setPort(publisherRegisterPb.getBaseRegister().getPort());
        publisherRegister.setProcessId(publisherRegisterPb.getBaseRegister().getProcessId());
        publisherRegister.setRegistId(publisherRegisterPb.getBaseRegister().getRegistId());
        publisherRegister.setTimestamp(publisherRegisterPb.getBaseRegister().getTimestamp());
        publisherRegister.setVersion(publisherRegisterPb.getBaseRegister().getVersion());
        publisherRegister.setDataList(DataBoxConvertor.convertBoxes2Javas(publisherRegisterPb.getDataListList()));
        publisherRegister.setAttributes(publisherRegisterPb.getBaseRegister().getAttributesMap());
        if (publisherRegisterPb.getBaseRegister().getZone() != null) {
            publisherRegister.setZone(publisherRegisterPb.getBaseRegister().getZone());
        }
        return publisherRegister;
    }
}
